<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-01-01
 * Time: 00:22
 */

namespace extend\oss;

use OSS\OssClient;
use OSS\Core\OssException;

class Upload extends Base
{
    public function string($string, $option = [])
    {
        if (!empty($string)) {
            $config = $this->config;
            try {
                $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);

                $exist = $ossClient->doesObjectExist($config['bucket'], $option['file_path']);
                // 判断文件是否覆盖
                if ($exist === true && $option['replace'] === false) {
                    return '源文件已存在';
                }
                // 上传文件
                $response = $ossClient->putObject($config['bucket'], $option['file_path'], $string);
                if (empty($option['type']) && !empty($response['oss-requestheaders']['Content-Type'])) {
                    $option['type'] = $response['oss-requestheaders']['Content-Type'];
                }
                return ['path' => $option['file_path'], 'url' => $config['url'] . '/' . $option['file_path'], 'type' => $option['type']];
            } catch (OssException $e) {
                return $e->getMessage();
            }
        } else {
            return '非法文件错误';
        }
    }

    /**
     * 上传文件
     * @param $file
     * @param array $option
     * @return array|string
     */
    public function file($file, $option = [])
    {

        if (!empty($file)) {
            $config = $this->config;
            // 生成文件名称
            $name = $this->buildName($file, $option);

            // 获取文件路径
            if (!empty($file->getPathName())) {
                $file = $file->getPathName();
            } else {
                $file = realpath($file);
            }
            $option['path'] .= '/' . $name;

            try {
                $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);
                $exist = $ossClient->doesObjectExist($config['bucket'], $option['path']);
                // 判断文件是否覆盖
                if ($exist === true && $option['replace'] === false) {
                    return '源文件已存在';
                }
                // 上传文件
                $response = $ossClient->uploadFile($config['bucket'], $option['path'], $file);

                if (empty($option['type']) && !empty($response['oss-requestheaders']['Content-Type'])) {
                    $option['type'] = $response['oss-requestheaders']['Content-Type'];
                }
                return ['path' => $option['path'], 'url' => $config['url'] . '/' . $option['path'], 'type' => $option['type']];
            } catch (OssException $e) {
                return $e->getMessage();
            }
        } else {
            return '非法文件错误';
        }
    }


    //测试上传本地视频
    function testUploadLocalVideo( $filePath, $option = [])
    {
        $config = $this->config;

        try {

            $ossClient = new OssClient($config['accessKeyId'], $config['accessKeySecret'], $config['endpoint']);

            $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
            $uploadVideoRequest = new UploadVideoRequest($filePath, 'testUploadLocalVideo via PHP-SDK');
            //$uploadVideoRequest->setCateId(1);
            //$uploadVideoRequest->setCoverURL("http://xxxx.jpg");
            //$uploadVideoRequest->setTags('test1,test2');
            //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
            //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289326');
            $userData = array(
                "MessageCallback"=>array("CallbackURL"=>"https://demo.sample.com/ProcessMessageCallback"),
                "Extend"=>array("localId"=>"xxx", "test"=>"www")
            );
            $uploadVideoRequest->setUserData(json_encode($userData));
            $res = $uploader->uploadLocalVideo($uploadVideoRequest);
            print_r($res);
        } catch (Exception $e) {
            printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
                $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
        }
    }


}